package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.assist.AssistBuild;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2.State;
import com.xcity.game.npc.NpcTemplate;
import com.xcity.game.player.robot.RobotBuild;
import com.xcity.game.player.robot.RobotUtils;
import com.xcity.pomelo.response.BuildV2Response.Assistant;
import com.xcity.pomelo.response.BuildV2Response.Invader;
import com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ViewPlayerHomeResponse extends AbstractViewPlayerResponse {

	public List<Building> buildings = Collections.emptyList();
	
	public static class Building {
		public int id;
		public String name;
		public String image;
		public State state;
		public Invader invader; // 侵占者
		public Assistant assistant;
		public List<EmployeeSimpleInfo> employees = Collections.emptyList();
		public Building(BuildV2 refer) {
			this.id = refer.getId();
			this.name = refer.getTemplate().getName();
			this.image = refer.getTemplate().getImage();
			this.state = refer.getState();
			
			Player invader = refer.currentInvader();
			Player owner = refer.getVm().getOwner(); 
			if (invader != null) {
				this.invader = new Invader(invader);
				owner = invader;
			}
			long[] eids = refer.currentEmployees();
			if (eids != null && eids.length > 0) {
				this.employees = new ArrayList<>(eids.length);
				for (long eid : eids) {
					Employee e;
					if (eid == 0L || (e = owner.getEmployees().get(eid)) == null) {
						this.employees.add(EmployeeSimpleInfo.EMPTY);
					} else {
						this.employees.add(new EmployeeSimpleInfo(e));
					}
				}
			}
			Player ass = refer.currentAssistant();
			if(ass != null){
				AssistBuild assbuild = ass.getAssists().findByOwnerId(owner.getId());
				this.assistant = new Assistant(ass, assbuild);
			}
		}
		public Building(RobotBuild refer) {
			this.id = refer.getId();
			this.name = refer.getTemplate().getName();
			this.image = refer.getTemplate().getImage();
			this.state = refer.getState();
			List<NpcTemplate> npcs = refer.getNpcs();
			if (!npcs.isEmpty()) {
				this.employees = new ArrayList<>(npcs.size());
				for (int i = 0; i < npcs.size(); i++) {
					NpcTemplate npc = npcs.get(i);
					this.employees.add(npc != null ? new EmployeeSimpleInfo(npc) : EmployeeSimpleInfo.EMPTY);
				}
			}
		}
	}
	
	public ViewPlayerHomeResponse(int serial, Player player) {
		super(serial, player);
		boolean isRobot = RobotUtils.isRobot(player);
		List<BuildV2> refers = player.getBuildings2().getBuildings();
		if (!refers.isEmpty()) {
			this.buildings = new ArrayList<>(refers.size());
			for (int i = 0; i < refers.size(); i++) {
				BuildV2 refer = refers.get(i);
				if (refer.getState() == State.BUILDING
						|| refer.getState() == State.BUILT
						|| refer.getState() == State.CAPTURED
						|| refer.getState() == State.ASSISTED) {
					this.buildings.add(isRobot ? new Building((RobotBuild) refer) : new Building(refer));
				}
			}
		}
	}

}
